package sms

import (
	"fmt"

	dysmsapi "github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
	"github.com/zeromicro/go-zero/core/logx"
)

type aliclient struct {
	secretId  string
	secretKey string
}

func NewSmsAliClient(id, key string) *aliclient {
	return &aliclient{
		secretId:  id,
		secretKey: key,
	}
}

func (s *aliclient) SendSms(phone string, template string, code string) error {
	client, err := dysmsapi.NewClientWithAccessKey("cn-qingdao", s.secretId, s.secretKey)
	if err != nil {
		return err
	}

	request := dysmsapi.CreateSendSmsRequest()
	request.Scheme = "https"

	request.PhoneNumbers = phone           //接收短信的手机号码
	request.SignName = "臣禾服务"              //短信签名名称
	request.TemplateCode = "SMS_238161767" //短信模板ID
	request.TemplateParam = fmt.Sprintf(`{"code":%s}`, code)

	response, err := client.SendSms(request)
	if err != nil {
		return err
	}
	logx.Infof("response is %#v\n", response)
	return nil
}
